// 基于crypto-js的aes CBC模式加密

import CryptoJS from 'crypto-js/core';
import encUtf8 from 'crypto-js/enc-utf8';
import aes from 'crypto-js/aes';
import Pkcs7 from 'crypto-js/pad-pkcs7';
import base64url from "base64url";
const mode = { // AES加密解密 CBC模式，AES-128bit, Pkcs7补码方式
  mode: CryptoJS.mode.CBC,
  padding: Pkcs7
};

export function encrypt(key, iv, word) {
  mode.iv = encUtf8.parse(iv);
  let srcs = encUtf8.parse(word);
  let encrypted = aes.encrypt(srcs, encUtf8.parse(key), mode);
  return base64url.fromBase64(encrypted.toString());
}

export function decrypt(key, iv, word) {
  mode.iv = encUtf8.parse(iv);
  let decrypt = aes.decrypt(base64url.toBase64(word), encUtf8.parse(key), mode);
  return encUtf8.stringify(decrypt).toString();
}
